package com.jnu.keepaccounts.record;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.jnu.keepaccounts.R;
import com.jnu.keepaccounts.data.DataTiaoMu;
import com.jnu.keepaccounts.data.XuanKa;

import java.util.ArrayList;
import java.util.List;

public class Tab extends AppCompatActivity  implements ShouZhiFragment.FragmentInteraction {
    TabLayout tabLayout;
    ViewPager viewPager;
    private ArrayList<XuanKa> xuanKas;
    private DataTiaoMu dataTiaoMu;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab);

        ActionBar actionBar = getSupportActionBar();
        if(actionBar!=null){
            actionBar.hide();
        }
        Intent intent = getIntent();
        this.xuanKas = intent.getExtras().getParcelableArrayList("xuanKas");
        this.dataTiaoMu  = (DataTiaoMu) intent.getSerializableExtra("tiaomu");

        tabLayout = findViewById(R.id.tab_layout);
        viewPager = findViewById(R.id.view_pager);

        ImageButton imageButton = findViewById(R.id.image_back);
        imageButton.setOnClickListener(v -> {
            finish();
        });

        Button button_again = findViewById(R.id.button_again);
        button_again.setOnClickListener(v -> {
            Toast.makeText(this,"暂时没做",Toast.LENGTH_LONG).show();
        });

        ArrayList<String> arrayList = new ArrayList<>();

        arrayList.add("收支");
        arrayList.add("转账");

        prepareViewPager(viewPager,arrayList);

        tabLayout.setupWithViewPager(viewPager);

    }
    public void process(DataTiaoMu dataTiaoMu){
        Intent intent = new Intent();
        if(dataTiaoMu == null)setResult(2,null);
        else{
            if(dataTiaoMu.getShuE()==0)intent = null;
            else{
                intent.putExtra("year",dataTiaoMu.getYear());
                intent.putExtra("month",dataTiaoMu.getMonth());
                intent.putExtra("day",dataTiaoMu.getDay());
                intent.putExtra("shuE",dataTiaoMu.getShuE());
                intent.putExtra("Name",dataTiaoMu.getXiangMu());
                intent.putExtra("pictureId",dataTiaoMu.getImage());
                intent.putExtra("source",dataTiaoMu.getLaiYuan());
                intent.putExtra("position", getIntent().getIntExtra("position",0));
                intent.putExtra("yusuan",dataTiaoMu.getYusuan());
                intent.putExtra("date",dataTiaoMu.getDate());
            }
//            Toast.makeText(this,"aaa",Toast.LENGTH_SHORT).show();
//        intent.putExtra("oldDataTiaoMu",this.dataTiaoMu);
            setResult(1,intent);
        }

        Tab.this.finish();
    }

    private void prepareViewPager(ViewPager viewPager, ArrayList<String> arrayList) {
        MainAdapter adapter = new MainAdapter(getSupportFragmentManager());

        ShouZhiFragment fragment = new ShouZhiFragment();
        ZhuanZhangFragment zhuanZhangFragment = new ZhuanZhangFragment();
//        收支fragment
        Bundle bundle = new Bundle();

        bundle.putString("title",arrayList.get(0));

        bundle.putParcelableArrayList("xuanKas",xuanKas);

        bundle.putSerializable("tiaomu",dataTiaoMu);

        fragment.setArguments(bundle);

        adapter.addFragment(fragment,arrayList.get(0));

        //fragment = new ShouZhiFragment()   ;

//        转账fragment
        Bundle bundle1 = new Bundle();

        bundle1.putString("title",arrayList.get(1));

        zhuanZhangFragment.setArguments(bundle1);

        adapter.addFragment(zhuanZhangFragment,arrayList.get(1));

        //zhuanZhangFragment = new ZhuanZhangFragment();

        viewPager.setAdapter(adapter);
    }

    public static class MainAdapter extends FragmentPagerAdapter{
        ArrayList<String> arrayList = new ArrayList<>();

        List<Fragment> fragmentList = new ArrayList<>();
        public void addFragment(Fragment fragment,String title){
            arrayList.add(title);
            fragmentList.add(fragment);
        }
        public MainAdapter(FragmentManager fragmentManager){
            super(fragmentManager) ;
        }
        @NonNull
        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }
        public  CharSequence getPageTitle(int position){
            return arrayList.get(position);
        }
    }

}
